//
//  JLNewFeatureCollectionViewController.m
//  JL网易彩票progress
//
//  Created by sunjinliang on 15/11/8.
//  Copyright © 2015年 sunjinliang. All rights reserved.
//

#import "JLNewFeatureCollectionViewController.h"
#import "JLTabBarController.h"
@interface JLNewFeatureCollectionViewController()
@property(nonatomic,weak)UIImageView * guide;
@property(nonatomic,weak)UIImageView * largeText;
@property(nonatomic,weak)UIImageView * smallText;
@property(nonatomic,assign) NSInteger curPage;
@end
@implementation JLNewFeatureCollectionViewController
-(void)viewDidLoad{
    [super viewDidLoad];
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    self.collectionView.pagingEnabled=YES;
    self.collectionView.bounces=NO;
    self.collectionView.showsHorizontalScrollIndicator=NO;
    [self setupChildView];
    self.curPage=1;
}
-(void)setupChildView{
    //添加引导线
    UIImageView * guideLine=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guideLine"]];
    guideLine.x=-210;
    [self.collectionView addSubview:guideLine];
    //添加大图
    UIImageView * guide=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guide1"]];

    guide.centerX=JL_WIDTH*0.5;
    [self.collectionView addSubview:guide];
    self.guide=guide;
    //添加大文本
    UIImageView * LargeText=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guideLargeText1"]];

    LargeText.centerX=JL_WIDTH*0.5;
    LargeText.centerY=JL_HEIGHT*0.7;
    [self.collectionView addSubview:LargeText];
    self.largeText =LargeText;
    //添加小文本
    UIImageView * smallText=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"guideSmallText1"]];

    smallText.centerX=JL_WIDTH*0.5;
    smallText.centerY=JL_HEIGHT*0.8;
    
    [self.collectionView addSubview:smallText];
    self.smallText =smallText;
    
    
    
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    //1计算当前页数
    NSInteger page=scrollView.contentOffset.x/self.view.bounds.size.width+1;
    //判断页数是否改变
    if (self.curPage!=page) {
        
    self.curPage=page;
    
    //2更改图片名称
    NSString * guideName=[NSString stringWithFormat:@"guide%tu",page];
    NSString * largeText=[NSString stringWithFormat:@"guideLargeText%tu",page];
    NSString * smallText=[NSString stringWithFormat:@"guideSmallText%tu",page];
    //3更换图片
    self.guide.image=[UIImage imageNamed:guideName];
    self.largeText.image=[UIImage imageNamed:largeText];
    self.smallText.image=[UIImage imageNamed:smallText];
    //4更改3个控件的位置
    CGFloat width=JL_WIDTH;
    if (scrollView.contentOffset.x<self.guide.x) {
        width=-width;
    }
        self.guide.centerX+=2*width;

        self.largeText.centerX+=2*width;
    
        self.smallText.centerX+=2*width;
    [UIView animateWithDuration:0.25 animations:^{

        self.guide.centerX-=width;
        

        self.largeText.centerX-=width;
        

        self.smallText.centerX-=width;
    }];
        
        if (page==4) {
            UIButton * btn=[[UIButton alloc]init];
            [btn setBackgroundImage:[UIImage imageNamed:@"guideStart"] forState:UIControlStateNormal];
            [btn sizeToFit];
            btn.centerX =JL_WIDTH*3.5;
            btn.y=JL_HEIGHT*0.9;
            
            [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
            [self.collectionView addSubview:btn];
        }
    }
}
-(void)btnClick{
    UIWindow * window=[UIApplication  sharedApplication].keyWindow;
 
    [window makeKeyAndVisible];
    JLTabBarController * tabBarVc=[[JLTabBarController alloc]init];
    window.rootViewController=tabBarVc;
    
    
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return 4;
}
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UIImageView *imgView=[[UIImageView alloc]init];
    imgView.image=[UIImage imageNamed:[NSString stringWithFormat:@"guide%tuBackground",indexPath.item+1]];
    imgView.frame=self.view.bounds;
    
    UICollectionViewCell * cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    //cell.frame=self.view.bounds;
    cell.backgroundColor=[UIColor redColor];
    [cell.contentView addSubview:imgView];
    
    return cell;
    
}
@end
